package com.huawei.hwid.update.b;

import android.content.Context;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.f.c;
import com.huawei.hwid.vermanager.VersionManager;
import com.huawei.secure.android.common.ssl.SecureSSLSocketFactory;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class b implements c {
    private volatile int a = -1;
    private HttpURLConnection b;

    public void a() {
        this.a = -1;
        if (this.b != null) {
            this.b.disconnect();
        }
    }

    public void b() {
        this.a = 1;
    }

    public int a(String str, OutputStream outputStream, int i, int i2, Context context) throws IOException, a {
        Throwable th;
        InputStream inputStream;
        Throwable th2;
        InputStream inputStream2 = null;
        try {
            a(str, context);
            this.b.setRequestMethod(HttpContants.HTTP_METHOD_GET);
            if (i > 0) {
                this.b.addRequestProperty("Range", "bytes=" + i + "-" + i2);
            }
            int responseCode = this.b.getResponseCode();
            if ((i > 0 && responseCode == 206) || (i <= 0 && responseCode == 200)) {
                inputStream2 = this.b.getInputStream();
                try {
                    a(new BufferedInputStream(inputStream2, 4096), outputStream);
                    outputStream.flush();
                } catch (Throwable th3) {
                    th = th3;
                    inputStream = inputStream2;
                    th2 = th;
                    c.a(inputStream);
                    throw th2;
                }
            }
            c.a(inputStream2);
            return responseCode;
        } catch (Throwable th32) {
            th = th32;
            inputStream = null;
            th2 = th;
            c.a(inputStream);
            throw th2;
        }
    }

    private void a(String str, Context context) throws IOException {
        if (this.a == 0) {
            e.d("HttpRequestHelper", "Not allowed to repeat open http(s) connection.", true);
        }
        this.b = (HttpURLConnection) new URL(str).openConnection();
        if (this.b instanceof HttpsURLConnection) {
            VersionManager.b().a((HttpsURLConnection) this.b, context);
            ((HttpsURLConnection) this.b).setHostnameVerifier(SecureSSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        }
        this.b.setConnectTimeout(30000);
        this.b.setReadTimeout(30000);
        this.b.setDoInput(true);
        this.b.setDoOutput(true);
        this.b.setUseCaches(false);
        this.a = 0;
    }

    private void a(InputStream inputStream, OutputStream outputStream) throws IOException, a {
        byte[] bArr = new byte[4096];
        do {
            int read = inputStream.read(bArr);
            if (-1 != read) {
                outputStream.write(bArr, 0, read);
            } else {
                return;
            }
        } while (this.a != 1);
        throw new a("HTTP(s) request was canceled.");
    }
}
